package com.weilai.common.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weilai.common.model.MedalRecord;
import com.weilai.common.model.Prize;
import com.weilai.common.model.RedeemCode;
import com.weilai.common.model.SysUser;
import com.weilai.common.service.MedalRecordService;
import com.weilai.common.service.PrizeService;
import com.weilai.common.service.RedeemCodeService;
import com.weilai.common.utils.StringRandom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static javax.swing.text.html.CSS.getAttribute;

/**
 * Created by Song on 2017/6/4.
 */
@Controller
@RequestMapping("/redeemCode")
public class RedeemCodeController {
    @Autowired
    RedeemCodeService redeemCodeService;
    @Autowired
    PrizeService prizeService;
    @Autowired
    MedalRecordService medalRecordService;
    /**
     * 跳转到添加奖章界面
     * @return
     */
    @RequestMapping("/toAddRedeemCode")
    public String toAddRedeemCode(){
        return "houtai/add_jiang";
    }

    /**
     * 跳转到奖章操作界面
     * @return
     */
    @RequestMapping("/toRedeemCodeOperation")
    public String toRedeemCodeOperation(){
        return "houtai/toRedeemCodeOperation";
    }

    /**
     * 生成6位奖章兑换码
     * @return
     */
    @RequestMapping("/create8Code")
    public String create8Code(Model model){
        String code = StringRandom.getStringRandom(6);
        RedeemCode redeemCode = new RedeemCode();
        redeemCode.setCode(code);
        //httpcode 界面用的状态码  200代表成功   400 代表失败
        model.addAttribute("redeemCode",redeemCode);
        model.addAttribute("httpCode","200");
        return "houtai/add_jiang";
//        if (redeemCodeService.addCode(redeemCode)== 1){
//
//        }else{
//            model.addAttribute("httpCode","400");
//            return "";
//        }

    }

    /**
     * 生成奖章兑换码
     * @return
     */
    //保存到数据库
    @RequestMapping("/addRedeemCode")
    public String addRedeemCode(Model model,HttpServletRequest request,@ModelAttribute RedeemCode redeemCode){
//            int count=(Integer) request.getAttribute("count");
//            redeemCode.setCount(count);
//            System.out.println("44444444444444444444"+redeemCode.getCode()+redeemCode.getCount());
            redeemCodeService.addCode(redeemCode);
            return "houtai/add_jiang";
    }

    /**
     * 删除兑换码
     * @param model
     * @param redeemCode
     * @return
     */
    @RequestMapping("/del8Code")
    public String del8Code(Model model,RedeemCode redeemCode){
        //httpcode 界面用的状态码  200代表成功   400 代表失败  （假的）
        if (redeemCodeService.del8Code(redeemCode)== 1){
            model.addAttribute("httpCode","200");
            model.addAttribute("code",StringRandom.getStringRandom(6));
        }else{
            model.addAttribute("httpCode","400");
        }
        return "redirect:/redeemCode/findAllCode";
    }

    /**
     * 修改兑换码
     * @param model
     * @param redeemCode
     * @return
     */
    @RequestMapping("/update8Code")
    public String update8Code(Model model,RedeemCode redeemCode){
        //httpcode 界面用的状态码  200代表成功   400 代表失败  （假的）
        if (redeemCodeService.update8Code(redeemCode)== 1){
            model.addAttribute("httpCode","200");
            model.addAttribute("code",StringRandom.getStringRandom(8));
        }else{
            model.addAttribute("httpCode","400");
        }
        return "";
    }

    /**
     * 查找所有奖章
     * @param model
     * @return
     */
    @RequestMapping("/findAllCode")
    public String findAllCode(Model model,@RequestParam(defaultValue = "1",required = false) int pageNum,
                              @RequestParam(defaultValue = "10",required = false) int pageSize){


        //赋值页面页码大小自动帮我们计算，我们只需要在页面上使用PageInfo的数据就行
        PageHelper.startPage(pageNum, pageSize);
        Page<RedeemCode> redeemCodeList = (Page) redeemCodeService.findAllCode(new RedeemCode());
        //分页信息
        PageInfo<RedeemCode> pageInfo = new PageInfo(redeemCodeList);
        model.addAttribute("page",pageInfo);

//        List<RedeemCode> redeemCodeList = redeemCodeService.findAllCode(new RedeemCode());
//        model.addAttribute("redeemCodeList",redeemCodeList);
////        for (RedeemCode re:redeemCodeList) {
////            System.out.println("code=====77777=============================="+re.getCode());
////        }
        return "houtai/jiang_operation";
    }

    /**
     * 兑换奖章
     */
    @RequestMapping(value = "frontduihuanjiangzhang", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Integer> duihuanjiangzhang(@RequestBody RedeemCode code, HttpServletRequest request) {
        Map map = new HashMap();
        //通过兑换码查找  存在兑换码 可以兑换奖章
        RedeemCode redeemCode = redeemCodeService.findByCode(code);
        if(redeemCode != null && !redeemCode.equals("")){
            //可以兑换
            //得到当前用户
            SysUser user = (SysUser) request.getSession().getAttribute("user");
//            SysUser user = new SysUser();
//            user.setId(2);
            //为奖章记录表添加纪录
            MedalRecord medalRecord = new MedalRecord();
            medalRecord.setDuihuanTime(new Date());
            medalRecord.setUseType("1");
            medalRecord.setCount(redeemCode.getCount());
            //// TODO: 2017/6/17
            //查找当前用户的奖章数量
            medalRecord.setUser(user);
            MedalRecord record = medalRecordService.findByUserOfMedalRecordCount(medalRecord);
            if(record == null){
                record.setCount(0);
            }
            medalRecord.setMedalCount(record.getMedalCount()+redeemCode.getCount());
            medalRecordService.addMedalCount(medalRecord);
            map.put("Httpcode",1);
            map.put("count",redeemCode.getCount());
            redeemCodeService.deleteByCode(redeemCode);
           return  map;

        }else{
            map.put("Httpcode",0);
            map.put("count",0);
            return map;
        }
    }


}
